package com.group4.datafactorybackend.util;


import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.http.HttpHeaders;

/**
 * @author xin
 * @date 2022/8/14 11:20
 */
public class HttpRequestUtils {

    /**
     * post
     */
    public static String postBody(HttpHeaders header, StringBuilder url, Integer timeout, String data){
        JSONObject json = JSONUtil.parseObj(data);
        return HttpRequest.post(String.valueOf(url))
                //头信息，多个头信息多次调用此方法即可
                .header(header)
                .body(json)
                //超时，毫秒
                .timeout((timeout)*1000)
                .execute().body();
    }

    /**
     * get
     */
    public static String getParameter(HttpHeaders header,StringBuilder url,Integer timeout){
        String result = HttpRequest.get(String.valueOf(url))
                //头信息，多个头信息多次调用此方法即可
                .header(header)
                .form(null)
                //超时，毫秒
                .timeout((timeout) * 1000)
                .execute().body();
        return result;
    }
}
